import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

import 'login_page.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterState createState() => new _RegisterState();
}

class _RegisterState extends State<RegisterPage> {
  TextEditingController _phoneController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  TextEditingController _mailController = new TextEditingController();
  TextEditingController _nameController = new TextEditingController();
  String sex = "male";

  @override
  Widget build(BuildContext context) {
    final logo = Text(
      "PCP",
      textAlign: TextAlign.center,
      style: new TextStyle(
        color: Colors.blue,
        fontSize: 50.0,
        fontWeight: FontWeight.bold,
      ),
    );

    final phone = TextFormField(
      keyboardType: TextInputType.phone,
      autofocus: false,
      //initialValue: '15688839826',
      controller: _phoneController,
      decoration: InputDecoration(
          hintText: 'PhoneNumber',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final password = TextFormField(
      autofocus: false,
      //initialValue: '请输入密码',
      obscureText: true,
      controller: _pwdController,
      decoration: InputDecoration(
          hintText: 'Password',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final mail = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      //initialValue: '15688839826',
      controller: _mailController,
      decoration: InputDecoration(
          hintText: 'email',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final userName = TextFormField(
      keyboardType: TextInputType.text,
      autofocus: false,
      //initialValue: '15688839826',
      controller: _nameController,
      decoration: InputDecoration(
          hintText: '用户名',
          contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
    );

    final Sex = Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text("性别："),
        SizedBox(width: 20),
        Text("男"),
        Radio(
          value: "male",
          groupValue: this.sex,
          onChanged: (value) {
            setState(() {
              this.sex = value;
            });
          },
        ),
        SizedBox(width: 20),
        Text("女"),
        Radio(
          value: "female",
          groupValue: this.sex,
          onChanged: (value) {
            setState(() {
              this.sex = value;
            });
          },
        )
      ],
    );

    final registerButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () async {
//            print(_phoneController.text);
//            print(_pwdController.text);
            try {
              Response response = await Dio().post(
                  "http://39.97.101.97:3000/user/addUser",
                  data: {
                    "email" : _mailController.text,
                    "password" : _pwdController.text,
                    "phoneNumber" : _phoneController.text,
                    "userName" : _nameController.text,
                    "sex" : sex.toString(),
                    "occupation" : "",
                    "company" : "",
                    "about" : "",
                    "salt" : ""
                  }
              );
//              print(response);
              if(response.data == "注册成功") {
                showToast(response.data);
                Navigator.of(context).push(new MaterialPageRoute(
                    builder: (BuildContext context) => new LoginPage()));
              } else
                showToast(response.data);
            } catch (e) {
              print(e);
            }
            Navigator.of(context).push(new MaterialPageRoute(
                builder: (BuildContext context) => new LoginPage()));
          },
          color: Colors.green,
          child: Text(
            '注 册',
            style: TextStyle(color: Colors.white, fontSize: 20.0),
          ),
        ),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        elevation: 0,
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          child: ListView(
            shrinkWrap: true,
            padding: EdgeInsets.only(left: 24.0, right: 24.0),
            children: <Widget>[
              logo,
              SizedBox(
                height: 48.0,
              ),
              mail,
              SizedBox(
                height: 8.0,
              ),
              password,
              SizedBox(
                height: 8.0,
              ),
              phone,
              SizedBox(
                height: 8.0,
              ),
              userName,
              SizedBox(
                height: 8.0,
              ),
              Sex,
              SizedBox(
                height: 8.0,
              ),
              registerButton,
            ],
          ),
        ),
      ),
    );
  }
}
